/**
 * 
 */
package br.org.bertol.mestrado;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.CountDownLatch;

import br.org.bertol.mestrado.engine.EngineAnt;
import br.org.bertol.mestrado.engine.Objective;
import br.org.bertol.mestrado.util.Configurator;

/**
 * @author contaqualquer
 */
public class StarterAnt extends AbstractStarter<EngineAnt> {

    /**
     * @param configurator
     *            Portador das mensagens
     */
    public StarterAnt(final Configurator configurator) {
        super(configurator);
    }

    public void startEngine() throws InterruptedException {
        startAntEngineBySystem(configurator.getIteracoes(), configurator
                .getSourcePath());
    }

    /**
     * @param avaliacoes
     * @param sourePath
     * @throws InterruptedException
     */
    public final void startAntEngineBySystem(final int avaliacoes,
            final String sourePath) throws InterruptedException {
        for (String system : configurator.getSystems()) {
            startAntEngineByInput(avaliacoes, sourePath, system);
        }
    }

    /**
     * @param avaliacoes
     * @param sourePath
     * @param system
     * @throws InterruptedException
     */
    public void startAntEngineByInput(final int avaliacoes,
            final String sourePath, final String system)
            throws InterruptedException {
        for (Objective[] objectives : configurator.getObjectives()) {
            startAntEngine(objectives, avaliacoes, sourePath, system);
        }
    }

    /**
     * @param avaliacoes
     * @param sourePath
     * @param system
     * @param constraints
     * @throws InterruptedException
     */
    public void startAntEngine(final Objective[] constraints,
            final int avaliacoes,
            final String sourePath, final String system)
            throws InterruptedException {

        final CountDownLatch systemstart = new CountDownLatch(1);

        final CountDownLatch systemstop = new CountDownLatch(1);

        final EngineAnt engineAnt = new EngineAnt(systemstart, systemstop,
                avaliacoes, constraints, sourePath, system,
                configurator.getWellThreshoold(), configurator
                .getTaxEvaporation(),
                configurator.getMinPheromone(), configurator.getFactorAlpha(),
                configurator.getFactorBeta());

        engineAnt.start();

        // notifica threads que
        // buscas podem iniciar
        systemstart.countDown();

        // coloca sistema em modo de espera
        // pela conclusão das buscas
        systemstop.await();

        // adiciona na lista de soluções
        engineList.add(engineAnt);

    }

    /**
     * {@inheritDoc}
     * @see br.org.bertol.mestrado.AbstractStarter#save()
     */
    @Override
    public void save() throws IOException {
        final String fileName = configurator.getSourcePath() + "/"
                + configurator.getOutPutFile();

        final PrintWriter outFile = new PrintWriter(new BufferedWriter(
                new FileWriter(fileName, true)));

        outFile.println("RESULTADO AntColonyOptimization");

        for (EngineAnt engineAnt : engineList) {
            engineAnt.writeToFile(outFile);
        }

        outFile.close();
    }

}
